﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using KD.Sms.Task;

namespace KD.Sms.Monitor
{
    public partial class frmElecMotor : Form
    {
        bool _bIsBusy = false;
        public frmElecMotor()
        {
            InitializeComponent();
        }

        private void frmElecMotor_Load(object sender, EventArgs e)
        {
            Task.Cyj.Instance.PlcDataReadOut += OnPlcDataReadOut;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="send"></param>
        /// <param name="e"></param>
        private void OnPlcDataReadOut(object send, Task.ActiveReadEventArgs e)
        {
            if (_bIsBusy)
                return;
            try
            {
                _bIsBusy = true;
                short[] _arrReads = (short[])e.E_ReadValue;
                int[] _aryBits350 = DataConvert.Ushort2Binary((int)(_arrReads[50 / 2]));
                int[] _aryBits352 = DataConvert.Ushort2Binary((int)(_arrReads[52 / 2]));
                int[] _aryBits376 = DataConvert.Ushort2Binary((int)(_arrReads[76 / 2]));
                if (_aryBits376[0] == 1)
                    rseDC.C_Value = UI.ThreeStateValue.Error;
                else
                {
                    if (_aryBits350[0] == 1)
                        rseDC.C_Value = UI.ThreeStateValue.Run;
                    else
                        rseDC.C_Value = UI.ThreeStateValue.Stop;
                }

                if (_aryBits376[1] == 1)
                    rseXC.C_Value = UI.ThreeStateValue.Error;
                else
                {
                    if (_aryBits350[1] == 1)
                        rseXC.C_Value = UI.ThreeStateValue.Run;
                    else
                        rseXC.C_Value = UI.ThreeStateValue.Stop;
                }

                if (_aryBits376[2] == 1)
                    rseSJ.C_Value = UI.ThreeStateValue.Error;
                else
                {
                    if (_aryBits350[2] == 1)
                        rseSJ.C_Value = UI.ThreeStateValue.Run;
                    else
                        rseSJ.C_Value = UI.ThreeStateValue.Stop;
                }

                if (_aryBits376[3] == 1)
                    rseLX.C_Value = UI.ThreeStateValue.Error;
                else
                {
                    if (_aryBits350[3] == 1)
                        rseLX.C_Value = UI.ThreeStateValue.Run;
                    else
                        rseLX.C_Value = UI.ThreeStateValue.Stop;
                }

                if (_aryBits376[5] == 1)
                    rseDJLDM.C_Value = UI.ThreeStateValue.Error;
                else
                {
                    if (_aryBits350[5] == 1)
                        rseDJLDM.C_Value = UI.ThreeStateValue.Run;
                    else
                        rseDJLDM.C_Value = UI.ThreeStateValue.Stop;
                }

                if (_aryBits376[7] == 1)
                    rseGLPD.C_Value = UI.ThreeStateValue.Error;
                else
                {
                    if (_aryBits350[7] == 1)
                        rseGLPD.C_Value = UI.ThreeStateValue.Run;
                    else
                        rseGLPD.C_Value = UI.ThreeStateValue.Stop;
                }

                if (_aryBits376[10] == 1)
                    rseSFPD.C_Value = UI.ThreeStateValue.Error;
                else
                {
                    if (_aryBits350[10] == 1)
                        rseSFPD.C_Value = UI.ThreeStateValue.Run;
                    else
                        rseSFPD.C_Value = UI.ThreeStateValue.Stop;
                }

                if (_aryBits376[11] == 1)
                    rseSFQ.C_Value = UI.ThreeStateValue.Error;
                else
                {
                    if (_aryBits350[11] == 1)
                        rseSFQ.C_Value = UI.ThreeStateValue.Run;
                    else
                        rseSFQ.C_Value = UI.ThreeStateValue.Stop;
                }

                if (_aryBits376[12] == 1)
                    rseJYQ.C_Value = UI.ThreeStateValue.Error;
                else
                {
                    if (_aryBits350[12] == 1)
                        rseJYQ.C_Value = UI.ThreeStateValue.Run;
                    else
                        rseJYQ.C_Value = UI.ThreeStateValue.Stop;
                }

                if (_aryBits376[13] == 1)
                    rseJYQM.C_Value = UI.ThreeStateValue.Error;
                else
                {
                    if (_aryBits350[13] == 1)
                        rseJYQM.C_Value = UI.ThreeStateValue.Run;
                    else
                        rseJYQM.C_Value = UI.ThreeStateValue.Stop;
                }

                if (_aryBits376[14] == 1)
                    resQLDM.C_Value = UI.ThreeStateValue.Error;
                else
                {
                    if (_aryBits350[14] == 1)
                        resQLDM.C_Value = UI.ThreeStateValue.Run;
                    else
                        resQLDM.C_Value = UI.ThreeStateValue.Stop;
                }


                resKZDY.C_Value = _aryBits352[13] == 0 ? UI.ThreeStateValue.Stop : UI.ThreeStateValue.Run;
                resZLDY.C_Value = _aryBits352[14] == 0 ? UI.ThreeStateValue.Stop : UI.ThreeStateValue.Run;
                rsePLC.C_Value = _aryBits352[15] == 0 ? UI.ThreeStateValue.Stop : UI.ThreeStateValue.Run;

            }
            catch (Exception ex)
            {
                FormHelper.ApplicationShow(UI.MsgType.Error, "电机状态解析出错!详见日志!");
                Log.LogHelper.DoWriteLogTypeSubitem(Log.LogType.Error, Log.LogSubitem.Forms, "电机状态解析出错!");
                Log.LogHelper.DoWriteLogTypeSubitem(Log.LogType.Error, Log.LogSubitem.Forms, ex.ToString());
            }
            finally
            {
                _bIsBusy = false;
            }
        }



    }
}
